package com.carpedil.auth.controller;

import com.carpedil.dto.UserDto;
import com.carpedil.jwt.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/** @author Administrator */
@RestController
public class AuthenticationController {

  @Autowired RestTemplate restTemplate;

  @GetMapping("/auth/{username}/{password}")
  public String getAuthentication(
      @PathVariable("username") String username, @PathVariable("password") String password) {
    System.out.println("username = " + username);
    System.out.println("password = " + password);

    UserDto userDto = new UserDto().setUsername(username).setPassword(password);
    //    String url = "http://127.0.0.1:8080/user/info/"+username+"/"+password;
    String url = "http://127.0.0.1:8080/user/info";
    UserDto user = restTemplate.postForEntity(url, userDto, UserDto.class).getBody();
    System.out.println("user = " + user);

    if (user != null) {
      return JwtUtil.createToken(
          new UserDto().setUsername(username).setPassword(password).setPerm(user.getPerm()));
    }
    return null;
  }
}
